<HTML>
<HEAD>
<TITLE>[Chapter 9] 9.3 Checkbox</TITLE>
<META NAME="author" CONTENT="John Zukowski">
<META NAME="date" CONTENT="Thu Jul 31 14:44:29 1997">
<META NAME="form" CONTENT="html">
<META NAME="metadata" CONTENT="dublincore.0.1">
<META NAME="objecttype" CONTENT="book part">
<META NAME="otheragent" CONTENT="gmat dbtohtml">
<META NAME="publisher" CONTENT="O'Reilly &amp; Associates, Inc.">
<META NAME="source" CONTENT="SGML">
<META NAME="subject" CONTENT="Java AWT">
<META NAME="title" CONTENT="Java AWT">
<META HTTP-EQUIV="Content-Script-Type" CONTENT="text/javascript">
</HEAD>
<body vlink="#551a8b" alink="#ff0000" text="#000000" bgcolor="#FFFFFF" link="#0000ee">

<DIV CLASS=htmlnav>
<H1><a href='index.htm'><IMG SRC="gifs/smbanner.gif"
     ALT="Java AWT" border=0></a></H1>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch09_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><B><FONT FACE="ARIEL,HELVETICA,HELV,SANSERIF" SIZE="-1">Chapter 9<br>Pick Me</FONT></B></TD>
<td width=172 align=right valign=top><A HREF="ch09_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
</table>

&nbsp;
<hr align=left width=515>
</DIV>
<DIV CLASS=sect1>
<h2 CLASS=sect1><A CLASS="TITLE" NAME="JAWT-CH-9-SECT-3">9.3 Checkbox</A></h2>

<P CLASS=para>
<A NAME="CH09.CHECK1"></A><A NAME="CH09.CHECK2"></A>The <tt CLASS=literal>Checkbox</tt> is a general purpose 
way to record a <tt CLASS=literal>true</tt> or <tt CLASS=literal>false</tt> state. When several checkboxes are associated 
in a <tt CLASS=literal>CheckboxGroup</tt> (<A HREF="ch09_04.htm#JAWT-CH-9-SECT-4">CheckboxGroup</A>), 
only one can be selected at a time; selecting each <tt CLASS=literal>Checkbox</tt> 
causes the previous selection to become deselected. The <tt CLASS=literal>CheckboxGroup</tt> 
is Java's way of offering the interface element known as radio 
buttons or a radio box. When you create a <tt CLASS=literal>Checkbox</tt>, 
you decide whether to place it into a <tt CLASS=literal>CheckboxGroup</tt> 
by setting the proper argument in its constructor. 

<P CLASS=para>
Every <tt CLASS=literal>Checkbox</tt> has both a label 
and a state, although the label could be empty. You can change the label 
based on the state of the <tt CLASS=literal>Checkbox</tt>. 
<A HREF="ch09_03.htm#JAWT-CH-9-FIG-4">Figure 9.4</A> shows what several <tt CLASS=literal>Checkbox</tt> 
components might look like. The two on the left are independent, while 
the five on the right are in a <tt CLASS=literal>CheckboxGroup</tt>. 
Note that the appearance of a <tt CLASS=literal>Checkbox</tt> 
varies quite a bit from platform to platform. However, the appearance of 
a <tt CLASS=literal>CheckboxGroup</tt> is always different 
from the appearance of an ungrouped <tt CLASS=literal>Checkbox</tt>, 
and the appearance of a checked <tt CLASS=literal>Checkbox</tt> 
is different from an unchecked <tt CLASS=literal>Checkbox</tt>. 

<DIV CLASS=figure>
<h4 CLASS=figure><A CLASS="TITLE" NAME="JAWT-CH-9-FIG-4">Figure 9.4: Two separate checkboxes and a CheckboxGroup</A></h4>


<p>
<img align=middle src="./figs/jawt0904.gif" alt="[Graphic: Figure 9-4]" width=260 height=185 border=0>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-9-SECT-3.1">Checkbox Methods</A></h3>Constructors

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Checkbox () </I><br>
<DD>

<P CLASS=para>
This constructor for <tt CLASS=literal>Checkbox</tt> 
creates a new instance with no label or grouping. The initial state of 
the item is <tt CLASS=literal>false</tt>. A checkbox 
doesn't necessarily need a label; however, a checkbox without a label 
might be confusing, unless it is being used as a column in a table or a 
spreadsheet. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Checkbox (String label) </I><br>
<DD>

<P CLASS=para>
The second constructor creates a new <tt CLASS=literal>Checkbox</tt> 
with a label of <tt CLASS=literal>label</tt> and no 
grouping. The initial state of the item is <tt CLASS=literal>false</tt>. 
If you want a simple yes/no choice and plan to make no 
the default, use this constructor. If the <tt CLASS=literal>Checkbox</tt> 
will be in a group or you want its initial value to be <tt CLASS=literal>true</tt>, 
use the next constructor. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Checkbox (String label, boolean state) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
This constructor allows you to specify the <tt CLASS=literal>Checkbox</tt>'s 
initial state. With it you create a <tt CLASS=literal>Checkbox</tt> 
with a label of <tt CLASS=literal>label</tt> and an 
initial state of <tt CLASS=literal>state</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public Checkbox (String label, boolean state, CheckboxGroup group) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br><I CLASS=emphasis>public Checkbox (String label, CheckboxGroup group, boolean state)</I><br>
<DD>

<P CLASS=para>
The final constructor for <tt CLASS=literal>Checkbox</tt> 
is the most flexible. With this constructor you create a <tt CLASS=literal>Checkbox</tt> 
with a label of <tt CLASS=literal>label</tt>, a <tt CLASS=literal>CheckboxGroup</tt> 
of <tt CLASS=literal>group</tt>, and an initial state 
of <tt CLASS=literal>state</tt>. If <tt CLASS=literal>group</tt> 
is <tt CLASS=literal>null</tt>, the <tt CLASS=literal>Checkbox</tt> 
is independent. 

<P CLASS=para>
In Java 1.0, you created an independent <tt CLASS=literal>Checkbox</tt> 
with an initial value of <tt CLASS=literal>true</tt> 
by using <tt CLASS=literal>null</tt> as the group: 

<DIV CLASS=screen>
<P>
<PRE>
Checkbox cb = new Checkbox ("Help", null, true)
</PRE>
</DIV>

<P CLASS=para>
The shape of the <tt CLASS=literal>Checkbox</tt> reflects 
whether it's in a <tt CLASS=literal>CheckboxGroup</tt> 
or independent. On Microsoft Windows, grouped checkboxes are represented 
as circles. On a UNIX system, they are diamonds. On both systems, independent 
checkboxes are squares. </DL>
Label

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public String getLabel () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getLabel()</tt> method retrieves 
the current label on the <tt CLASS=literal>Checkbox</tt> 
and returns it as a <tt CLASS=literal>String</tt> 
object. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void setLabel (String label) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setLabel()</tt> method changes 
the label of the <tt CLASS=literal>Checkbox</tt> to 
<tt CLASS=literal>label</tt>. If the new label is 
a different size than the old one, you have to <tt CLASS=literal>validate()</tt> 
the container after the change to ensure the entire label will be seen. </DL>
State

<P CLASS=para>
A state of <tt CLASS=literal>true</tt> means the <tt CLASS=literal>Checkbox</tt> 
is selected. A state of <tt CLASS=literal>false</tt> 
means that the <tt CLASS=literal>Checkbox</tt> is 
not selected. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean getState () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getState()</tt> method retrieves 
the current state of the <tt CLASS=literal>Checkbox</tt> 
and returns it as a boolean. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setState (boolean state) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setState()</tt> method changes 
the state of the <tt CLASS=literal>Checkbox</tt> to 
<tt CLASS=literal>state</tt>. If the <tt CLASS=literal>Checkbox</tt> 
is in a <tt CLASS=literal>CheckboxGroup</tt> and <tt CLASS=literal>state</tt> 
is true, the other items in the group become false. </DL>
ItemSelectable method

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public Objects[] getSelectedObjects () <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getSelectedObjects()</tt> method 
returns the <tt CLASS=literal>Checkbox</tt> label 
as a one-element <tt CLASS=literal>Object</tt> array 
if it is currently selected, or <tt CLASS=literal>null</tt> 
if the <tt CLASS=literal>Checkbox</tt> is not selected. 
Because this method is part of the <tt CLASS=literal>ItemSelectable</tt> 
interface, you can use it to look at the selected items in a <tt CLASS=literal>Choice</tt>, 
<tt CLASS=literal>List</tt>, or <tt CLASS=literal>Checkbox</tt>. </DL>
CheckboxGroup

<P CLASS=para>
This section lists methods that you issue to <tt CLASS=literal>Checkbox</tt> 
to affect its relationship to a <tt CLASS=literal>CheckboxGroup</tt>. 
Methods provided by the <tt CLASS=literal>CheckboxGroup</tt> 
itself can be found later in this chapter. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public CheckboxGroup getCheckboxGroup () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>getCheckboxGroup()</tt> method 
returns the current <tt CLASS=literal>CheckboxGroup</tt> 
for the <tt CLASS=literal>Checkbox</tt>. If the <tt CLASS=literal>Checkbox</tt> 
is not in a group, this method returns <tt CLASS=literal>null</tt>. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void setCheckboxGroup (CheckboxGroup group) </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>setCheckboxGroup()</tt> method 
allows you to insert a <tt CLASS=literal>Checkbox</tt> 
into a different <tt CLASS=literal>CheckboxGroup</tt>. 
To make the <tt CLASS=literal>Checkbox</tt> independent, 
pass a <tt CLASS=literal>group</tt> argument of <tt CLASS=literal>null</tt>. 
The method sets every <tt CLASS=literal>Checkbox</tt> 
in the original <tt CLASS=literal>CheckboxGroup</tt> 
to <tt CLASS=literal>false</tt> (<tt CLASS=literal>cb.getCheckboxGroup().setCurrent(null)</tt>), 
then the <tt CLASS=literal>Checkbox</tt> is added 
to the new group without changing any values in the new group. 

<P CLASS=para>
<tt CLASS=literal>Checkbox</tt> components take on 
a different shape when they are in a <tt CLASS=literal>CheckboxGroup</tt>. 
If the checkbox was originally not in a <tt CLASS=literal>CheckboxGroup</tt>, 
the shape of the checkbox does not change automatically when you put it 
in one with <tt CLASS=literal>setCheckboxGroup()</tt>. 
(This also holds when you remove a <tt CLASS=literal>Checkbox</tt> 
from a <tt CLASS=literal>CheckboxGroup</tt> and make 
it independent or vice versa.) In order for the <tt CLASS=literal>Checkbox</tt> 
to look right once added to <tt CLASS=literal>group</tt>, 
you need to destroy and create (<tt CLASS=literal>removeNotify()</tt> and <tt CLASS=literal>addNotify()</tt>, respectively) the 
<tt CLASS=literal>Checkbox</tt> peer to correct the 
shape. Also, it is possible to get multiple true <tt CLASS=literal>Checkbox</tt> 
components in <tt CLASS=literal>group</tt> this way, 
since the new <tt CLASS=literal>CheckboxGroup</tt>'s 
current selection does not get adjusted. To avoid this problem, make sure 
it is grouped properly the first time, or be sure to clear the selections 
with a call to <tt CLASS=literal>getCheckboxGroup().setCurrent(null)</tt>. </DL>
Miscellaneous methods

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public synchronized void addNotify () </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addNotify()</tt> method will 
create the <tt CLASS=literal>Checkbox</tt> 
peer in the appropriate shape. If you override this method, call <tt CLASS=literal>super.addNotify()</tt> 
first, then add your customizations for the new class. You will then be 
able to do everything you need with the information about the newly created 
peer. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected String paramString ()  </I><br>
<DD>

<P CLASS=para>
When you call the <tt CLASS=literal>toString()</tt> 
method of <tt CLASS=literal>Checkbox</tt>, the default 
<tt CLASS=literal>toString()</tt> method of <tt CLASS=literal>Component</tt> 
is called. This in turn calls <tt CLASS=literal>paramString()</tt> 
which builds up the string to display. At the <tt CLASS=literal>Checkbox</tt> 
level, the label (if non-null) and the state of the item are appended. 
Assuming the Dialog <tt CLASS=literal>Checkbox</tt> 
in <A HREF="ch09_03.htm#JAWT-CH-9-FIG-4">Figure 9.4</A> was selected, the results would be: </DL>
<DIV CLASS=screen>
<P>
<PRE>
java.awt.Checkbox[85,34,344x32,label=Dialog,state=true]
</PRE>
</DIV>

</DIV>

<DIV CLASS=sect2>
<h3 CLASS=sect2><A CLASS="TITLE" NAME="JAWT-CH-9-SECT-3.2">Checkbox Events</A></h3>

<P CLASS=para>
<A NAME="CH09.EVENT3"></A><A NAME="CH09.EVENT3A"></A>The primary event for a <tt CLASS=literal>Checkbox</tt> 
occurs when the user selects it. With the 1.0 event model, this generates 
an <tt CLASS=literal>ACTION_EVENT</tt> and triggers 
the <tt CLASS=literal>action()</tt> method. Once the 
<tt CLASS=literal>Checkbox</tt> has the input focus, 
the various keyboard events can be generated, but they do not serve any 
useful purpose because the <tt CLASS=literal>Checkbox</tt> 
doesn't change. The sole key of value for a <tt CLASS=literal>Checkbox</tt> 
is the spacebar. This may generate the <tt CLASS=literal>ACTION_EVENT</tt> 
after <tt CLASS=literal>KEY_PRESS</tt> and <tt CLASS=literal>KEY_RELEASE</tt>; 
thus the sequence of method calls would be <tt CLASS=literal>keyDown()</tt>, 
<tt CLASS=literal>keyUp()</tt>, and then <tt CLASS=literal>action()</tt>. 

<P CLASS=para>
With the version 1.1 event model, you register an <tt CLASS=literal>ItemListener</tt> 
with the method <tt CLASS=literal>addItemListener()</tt>. Then when the user selects the <tt CLASS=literal>Checkbox</tt>, 
the method <tt CLASS=literal>ItemListener.itemStateChanged()</tt> 
is called through the protected <tt CLASS=literal>Checkbox.processItemEvent()</tt> 
method. Key, mouse, and focus listeners are registered through the <tt CLASS=literal>Component</tt> 
methods of <tt CLASS=literal>addKeyListener()</tt>, 
<tt CLASS=literal>addMouseListener()</tt>, and <tt CLASS=literal>addFocusListener()</tt>, 
respectively. Action

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean action (Event e, Object o)</I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>action()</tt> method for a <tt CLASS=literal>Checkbox</tt> 
is called when the user selects it. <tt CLASS=literal>e</tt> 
is the <tt CLASS=literal>Event</tt> instance for the 
specific event, while <tt CLASS=literal>o</tt> 
is the opposite of the old state of the toggle. If the <tt CLASS=literal>Checkbox</tt> 
was <tt CLASS=literal>true</tt> when it was selected, <tt CLASS=literal>o</tt> 
will be <tt CLASS=literal>false</tt>. Likewise, if it was <tt CLASS=literal>false</tt>, <tt CLASS=literal>o</tt> will be <tt CLASS=literal>true</tt>. This incantation 
sounds unnecessarily complex, and for a single <tt CLASS=literal>Checkbox</tt>, 
it is: <tt CLASS=literal>o</tt> is just the new state 
of the <tt CLASS=literal>Checkbox</tt>. The following 
code uses <tt CLASS=literal>action()</tt> with a single 
<tt CLASS=literal>Checkbox</tt>. 

<DIV CLASS=screen>
<P>
<PRE>
public boolean action (Event e, Object o) {
    if (e.target instanceof Checkbox) {
        System.out.println ("Checkbox is now " + o);
    }
    return false;
}
</PRE>
</DIV>

<P CLASS=para>
On the other hand, if the <tt CLASS=literal>Checkbox</tt> 
is in a <tt CLASS=literal>CheckboxGroup</tt>, <tt CLASS=literal>o</tt> 
is still the opposite of the old state of the toggle, which may or may 
not be the new state of the <tt CLASS=literal>Checkbox</tt>. 
If the <tt CLASS=literal>Checkbox</tt> is initially 
<tt CLASS=literal>false</tt>, <tt CLASS=literal>o</tt> 
will be <tt CLASS=literal>true</tt>, and the <tt CLASS=literal>Checkbox</tt>'s 
new state will be <tt CLASS=literal>true</tt>. However, 
if the <tt CLASS=literal>Checkbox</tt> is initially 
<tt CLASS=literal>true</tt>, selecting the <tt CLASS=literal>Checkbox</tt> 
doesn't change anything because one <tt CLASS=literal>Checkbox</tt> 
in the group must always be <tt CLASS=literal>true</tt>. 
In this case, <tt CLASS=literal>o</tt> is <tt CLASS=literal>false</tt> 
(the opposite of the old state), though the <tt CLASS=literal>Checkbox</tt>'s 
state remains <tt CLASS=literal>true</tt>. 

<P CLASS=para>
Therefore, if you're working with a <tt CLASS=literal>CheckboxGroup</tt> 
and need to do something once when the selection changes, perform 
your action only when <tt CLASS=literal>o</tt> is true. 
To find out which <tt CLASS=literal>Checkbox</tt> 
was actually chosen, you need to call the <tt CLASS=literal>getLabel()</tt> 
method for the target of event <tt CLASS=literal>e</tt>. 
(It would be nice if <tt CLASS=literal>o</tt> gave 
us the label of the <tt CLASS=literal>Checkbox</tt> 
that was selected, but it doesn't.) An example of this follows: 

<DIV CLASS=screen>
<P>
<PRE>
public boolean action (Event e, Object o) {
    if (e.target instanceof Checkbox) {
        System.out.println (((Checkbox)(e.target)).getLabel() +
            " was selected.");
        if (new Boolean (o.toString()).booleanValue()) {
            System.out.println ("New option chosen");
        } else {
            System.out.println ("Use re-selected option");
        }
    }
    return false;
}
</PRE>
</DIV>

</DL>
<P CLASS=para>
One other unfortunate twist of <tt CLASS=literal>CheckboxGroup</tt>: 
it would be nice if there was some easy way to find out about checkboxes 
that change state without selection--for example, if you could find 
out which <tt CLASS=literal>Checkbox</tt> was deselected 
when a new <tt CLASS=literal>Checkbox</tt> was selected. 
Unfortunately, you can't, except by keeping track of the state of 
all your checkboxes at all times. When a <tt CLASS=literal>Checkbox</tt> 
state becomes <tt CLASS=literal>false</tt> because another <tt CLASS=literal>Checkbox</tt> 
was selected, no additional event is generated, in either Java 1.0 or 1.1. Keyboard

<P CLASS=para>
<tt CLASS=literal>Checkbox</tt>es are able to capture 
keyboard-related events once the <tt CLASS=literal>Checkbox</tt> 
has the input focus, which happens when it is selected. If you can find 
a use for this, you can use <tt CLASS=literal>keyDown()</tt> 
and <tt CLASS=literal>keyUp()</tt>. For most interface 
designs I can think of, <tt CLASS=literal>action()</tt> 
is sufficient. A possible use for keyboard events is to jump to other <tt CLASS=literal>Checkbox</tt> 
options in a <tt CLASS=literal>CheckboxGroup</tt>, 
but I think that is more apt to confuse users than help. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean keyDown (Event e, int key)</I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>keyDown()</tt> method is called 
whenever the user presses a key while the <tt CLASS=literal>Checkbox</tt> 
has the input focus. <tt CLASS=literal>e</tt> is the 
<tt CLASS=literal>Event</tt> instance for the specific 
event, while <tt CLASS=literal>key</tt> 
is the integer representation of the character pressed. The identifier 
for the event (<tt CLASS=literal>e.id</tt>) 
for <tt CLASS=literal>keyDown()</tt> could be either 
<tt CLASS=literal>KEY_PRESS</tt> for a regular key 
or <tt CLASS=literal>KEY_ACTION</tt> for an action-oriented key (i.e., arrow or function key). There is no visible indication 
that the user has pressed a key over the checkbox. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public boolean keyUp (Event e, int key)</I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>keyUp()</tt> method is called 
whenever the user releases a key while the <tt CLASS=literal>Checkbox</tt> 
has the input focus. <tt CLASS=literal>e</tt> is the 
<tt CLASS=literal>Event</tt> instance for the specific 
event, while <tt CLASS=literal>key</tt> is 
the integer representation of the character pressed. The identifier for 
the event (<tt CLASS=literal>e.id</tt>) 
for <tt CLASS=literal>keyUp()</tt> could be either 
<tt CLASS=literal>KEY_RELEASE</tt> for a regular key 
or <tt CLASS=literal>KEY_ACTION_RELEASE</tt> for an 
action-oriented key (i.e., arrow or function key). <tt CLASS=literal>keyUp()</tt> 
may be used to determine how long <tt CLASS=literal>key</tt> 
has been pressed. </DL>
Mouse

<P CLASS=para>
Ordinarily, the <tt CLASS=literal>Checkbox</tt> component 
does not reliably trigger any mouse events. Focus

<P CLASS=para>
Ordinarily, the <tt CLASS=literal>Checkbox</tt> component 
does not reliably trigger any focus events. Listeners and 1.1 event handling

<P CLASS=para>
With the 1.1 event model, you register listeners, and they are told when 
the event happens. 

<P>
<DL CLASS=variablelist>
<DT CLASS=varlistentry><I CLASS=emphasis>public void addItemListener(ItemListener listener) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>addItemListener()</tt> method 
registers <tt CLASS=literal>listener</tt> as an object 
interested in being notified when an <tt CLASS=literal>ItemEvent</tt> 
passes through the <tt CLASS=literal>EventQueue</tt> 
with this <tt CLASS=literal>Checkbox</tt> as its target. 
Then, the <tt CLASS=literal>listener.itemStateChanged()</tt> 
method will be called. Multiple listeners can be registered. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>public void removeItemListener(ItemListener listener) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>removeItemListener()</tt> method 
removes <tt CLASS=literal>listener</tt> as a interested 
listener. If listener is not registered, nothing happens. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected void processEvent(AWTEvent e) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>processEvent()</tt> method receives 
every <tt CLASS=literal>AWTEvent</tt> with this <tt CLASS=literal>Checkbox</tt> 
as its target. <tt CLASS=literal>processEvent()</tt> 
then passes it along to any listeners for processing. When you subclass 
<tt CLASS=literal>Checkbox</tt>, overriding <tt CLASS=literal>processEvent()</tt> 
allows you to process all events yourself, before sending them to any listeners. 
In a way, overriding <tt CLASS=literal>processEvent()</tt> 
is like overriding <tt CLASS=literal>handleEvent()</tt> 
using the 1.0 event model. 

<P CLASS=para>
If you override <tt CLASS=literal>processEvent()</tt>, 
remember to call <tt CLASS=literal>super.processEvent(e)</tt> 
last to ensure that regular event processing can occur. If you want to 
process your own events, it's a good idea to call <tt CLASS=literal>enableEvents()</tt> 
(inherited from <tt CLASS=literal>Component</tt>) 
to ensure that events are delivered even in the absence of registered listeners. 

<p>
<DT CLASS=varlistentry><I CLASS=emphasis>protected void processItemEvent(ItemEvent e) <img src="gifs/bstar.gif" alt="(New)" border=0> </I><br>
<DD>

<P CLASS=para>
The <tt CLASS=literal>processItemEvent()</tt> method 
receives every <tt CLASS=literal>ItemEvent</tt> with 
this <tt CLASS=literal>Checkbox</tt> as its target. 
<tt CLASS=literal>processItemEvent()</tt> then passes 
it along to any listeners for processing. When you subclass <tt CLASS=literal>Checkbox</tt>, 
overriding <tt CLASS=literal>processItemEvent()</tt> 
allows you to process all events yourself, before sending them to any listeners. 
In a way, overriding <tt CLASS=literal>processItemEvent()</tt> 
is like overriding <tt CLASS=literal>action()</tt> using 
the 1.0 event model. 

<P CLASS=para>
If you override <tt CLASS=literal>processItemEvent()</tt>, 
remember to call the method <tt CLASS=literal>super.processItemEvent(e)</tt> 
last to ensure that regular event processing can occur. If you want to 
process your own events, it's a good idea to call <tt CLASS=literal>enableEvents()</tt> 
(inherited from <tt CLASS=literal>Component</tt>) 
to ensure that events are delivered even in the absence of registered listeners. </DL>
</DIV>

</DIV>


<DIV CLASS=htmlnav>

<P>
<HR align=left width=515>
<table width=515 border=0 cellpadding=0 cellspacing=0>
<tr>
<td width=172 align=left valign=top><A HREF="ch09_02.htm"><IMG SRC="gifs/txtpreva.gif" ALT="Previous" border=0></A></td>
<td width=171 align=center valign=top><a href="index.htm"><img src='gifs/txthome.gif' border=0 alt='Home'></a></td>
<td width=172 align=right valign=top><A HREF="ch09_04.htm"><IMG SRC="gifs/txtnexta.gif" ALT="Next" border=0></A></td>
</tr>
<tr>
<td width=172 align=left valign=top>Lists</td>
<td width=171 align=center valign=top><a href="index/idx_a.htm"><img src='gifs/index.gif' alt='Book Index' border=0></a></td>
<td width=172 align=right valign=top>CheckboxGroup</td>
</tr>
</table>
<hr align=left width=515>

<IMG SRC="gifs/smnavbar.gif" USEMAP="#map" BORDER=0> 
<MAP NAME="map"> 
<AREA SHAPE=RECT COORDS="0,0,108,15" HREF="../javanut/index.htm"
alt="Java in a Nutshell"> 
<AREA SHAPE=RECT COORDS="109,0,200,15" HREF="../langref/index.htm" 
alt="Java Language Reference"> 
<AREA SHAPE=RECT COORDS="203,0,290,15" HREF="../awt/index.htm" 
alt="Java AWT"> 
<AREA SHAPE=RECT COORDS="291,0,419,15" HREF="../fclass/index.htm" 
alt="Java Fundamental Classes"> 
<AREA SHAPE=RECT COORDS="421,0,514,15" HREF="../exp/index.htm" 
alt="Exploring Java"> 
</MAP>
</DIV>

</BODY>
</HTML>
